package com.leyou.search.controller;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.pojo.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @GetMapping("init")
    public ResponseEntity<String> searchInit(){
//        初始化索引库
        searchService.createIndexAndMapping();
//        加载数据
        searchService.loadData();
//        返回
        return ResponseEntity.ok("索引库初始化完成");
    }

    /**
     * 初始化索引库
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }
    /*
    * 分页查询商品列表
    * */
    @PostMapping("list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO paramDTO){

        return searchService.searchGoods(paramDTO);
    }
    /*
    * 自动补全查询商品信息
    *
    * */

    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){

        return searchService.getSuggestion(key);
    }

    /*
    * 顶部查询商品过滤
    * */
    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> getFilterList(@RequestBody SearchParamDTO paramDTO){
        return searchService.getFilterList(paramDTO);
    }
}
